﻿<%@ WebHandler Language="C#" Class="MedirRegistro_ImagePie_Handler" %>

using System;
using System.Web;
using System.Drawing;
using System.IO;

public class MedirRegistro_ImagePie_Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.Clear();

        if (!String.IsNullOrEmpty(context.Request.QueryString["id"]))
        {
            int id = Int32.Parse(context.Request.QueryString["id"]);

            // Now you have the id, do what you want with it, to get the right image
            // More than likely, just pass it to the method, that builds the image
            Image image = GetImage(id);

            // Of course set this to whatever your format is of the image
            context.Response.ContentType = "image/png";
            // Save the image to the OutputStream
            image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
        }
        else
        {
            context.Response.ContentType = "text/html";
            context.Response.Write("<p>Need a valid id</p>");
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    private Image GetImage(int id)
    {
        int Angulo = 1;
        if ((id >= 1) && (id <= 360))
        {
            Angulo = (id * 360) / 100;
        }
        System.Drawing.Bitmap mapaBits = new System.Drawing.Bitmap(37, 60);
        System.Drawing.Graphics figura = System.Drawing.Graphics.FromImage(mapaBits);

        // Fonts and styles
        Pen blackPen = new Pen(Color.DarkGray, 1);
        SolidBrush mySolidBrush = new SolidBrush(Color.DarkCyan);
        FontFamily myFontFamily = new FontFamily("Times New Roman");
        PointF myPointF = new PointF(50, 20);
        StringFormat myStringFormat = new StringFormat();

        // Draw image       
        //figura.DrawArc(blackPen, 1, 0, 35, 35, 0, Angulo);
        figura.DrawEllipse(blackPen, 1, 0, 35, 35);
        figura.FillPie(mySolidBrush, 1, 0, 35, 35, 0, Angulo);
        figura.DrawString(id.ToString() + "%", new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular), new SolidBrush(Color.Black), 2, 38);

        // create the image from the stream.
        MemoryStream stream = new MemoryStream();
        mapaBits.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        mapaBits = null;
        return Image.FromStream(stream);
    }    

}